package com.qiyuexinxi.chat.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qiyuexinxi.chat.domain.Attention;
import com.qiyuexinxi.chat.domain.Dynamic;
import com.qiyuexinxi.chat.domain.User;
import com.qiyuexinxi.chat.domain.UserRecommend;
import com.qiyuexinxi.chat.dto.request.*;
import com.qiyuexinxi.chat.dto.response.DynamicListResponse;
import com.qiyuexinxi.chat.dto.response.DynamicResponse;
import com.qiyuexinxi.chat.dto.vo.UserInfoVo;
import com.qiyuexinxi.chat.mapper.AttentionMapper;
import com.qiyuexinxi.chat.mapper.DynamicMapper;
import com.qiyuexinxi.chat.mapper.UserMapper;
import com.qiyuexinxi.chat.service.DynamicService;
import com.qiyuexinxi.chat.util.RelativeDateFormat;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DynamicServiceImpl implements DynamicService {

    @Autowired
    DynamicMapper dynamicMapper;
    @Autowired
    AttentionMapper attentionMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    ThumbInfoServiceImpl thumbInfoService;
    @Autowired
    UserServiceImpl userService;

    /*发布动态*/
    @Override
    public boolean addDynamic(DynamicAddRequest dynamicAddRequest){
        Dynamic dynamic = new Dynamic();
        BeanUtils.copyProperties(dynamicAddRequest,dynamic);
        dynamic.setPictureAssociationId(JSON.toJSONString(dynamicAddRequest.getPictureUrls()));
        int i = dynamicMapper.insertSelective(dynamic);
        if(i<=0){
            return false;
        }
        return true;
    }

    /*删除动态*/
    @Override
    public boolean deleteDynamic(DynamicDeleteRequest dynamicDeleteRequest){
        Dynamic dynamic = new Dynamic();
        BeanUtils.copyProperties(dynamicDeleteRequest,dynamic);
        dynamic.setDeleted(0);
        int i = dynamicMapper.updateByPrimaryKeySelective(dynamic);
        if(i<=0){
            return false;
        }
        return true;
    }

    /*我的动态列表*/
    @Override
    public DynamicListResponse getDynamicList(GetDynamicListRequest getDynamicListRequest){
        PageHelper.startPage(getDynamicListRequest.getPage(), getDynamicListRequest.getSize());
        DynamicListResponse dynamicListResponse = new DynamicListResponse();
        List<DynamicResponse> dynamicResponseList = new ArrayList<DynamicResponse>();
        List<Dynamic> dynamics = dynamicMapper.selectDynamicByUserId(getDynamicListRequest.getUserId());
        PageInfo<Dynamic> pageInfo = new PageInfo<Dynamic>(dynamics);
        pageInfo.getList().forEach(dynamic -> {
            DynamicResponse dynamicResponse = getDynamicResponse(dynamic,getDynamicListRequest.getUserId());
            dynamicResponseList.add(dynamicResponse);
        });
        dynamicListResponse.setDynamicList(dynamicResponseList);
        dynamicListResponse.setTotalNum(pageInfo.getTotal() + "");
        dynamicListResponse.setHasNextPage(pageInfo.isHasNextPage());
        return dynamicListResponse;
    }

    /*我关注的动态列表*/
    @Override
    public DynamicListResponse getDynamicAttentionList(GetDynamicListRequest getDynamicListRequest){
        PageHelper.startPage(getDynamicListRequest.getPage(), getDynamicListRequest.getSize());
        DynamicListResponse dynamicListResponse = new DynamicListResponse();
        List<DynamicResponse> dynamicResponseList = new ArrayList<DynamicResponse>();
        //找到我关注的用户列表
        List<Attention> attentionList = attentionMapper.selectAttentionStatusByUserId(getDynamicListRequest.getUserId());
        //获取我关注的用户ids
        List<Integer> userIds = attentionList.stream().map(Attention->Attention.getAttentionUser()).collect(Collectors.toList());
        //获取我关注的用户的动态列表
        List<Dynamic> dynamicList = dynamicMapper.selectDynamicListByUserIds(userIds);
        PageInfo<Dynamic> pageInfo = new PageInfo<Dynamic>(dynamicList);
        pageInfo.getList().forEach(dynamic -> {
            DynamicResponse dynamicResponse = getDynamicResponse(dynamic,getDynamicListRequest.getUserId());
            dynamicResponseList.add(dynamicResponse);
        });
        dynamicListResponse.setDynamicList(dynamicResponseList);
        dynamicListResponse.setTotalNum(pageInfo.getTotal() + "");
        dynamicListResponse.setHasNextPage(pageInfo.isHasNextPage());
        return dynamicListResponse;
    }

     /*根据性别获取与之性别相反的所有人的动态列表*/
    @Override
    public DynamicListResponse getAllDynamicListBySex(GetDynamicListSexRequest getDynamicListSexRequest){
        PageHelper.startPage(getDynamicListSexRequest.getPage(), getDynamicListSexRequest.getSize());
        DynamicListResponse dynamicListResponse = new DynamicListResponse();
        List<DynamicResponse> dynamicResponseList = new ArrayList<DynamicResponse>();
        //如果当前用户行表为男、则查出所有女性用户、反之则反。
        List<User> userList = userMapper.selectUserListBySex(getDynamicListSexRequest.getSex());
        List<Integer> userIds = userList.stream().map(User->User.getUserId()).collect(Collectors.toList());
        List<Dynamic> dynamicList = dynamicMapper.selectDynamicListByUserIds(userIds);
        PageInfo<Dynamic> pageInfo = new PageInfo<Dynamic>(dynamicList);
        pageInfo.getList().forEach(dynamic -> {
            DynamicResponse dynamicResponse = getDynamicResponse(dynamic,getDynamicListSexRequest.getUserId());
            dynamicResponseList.add(dynamicResponse);
        });
        dynamicListResponse.setDynamicList(dynamicResponseList);
        dynamicListResponse.setTotalNum(pageInfo.getTotal() + "");
        dynamicListResponse.setHasNextPage(pageInfo.isHasNextPage());
        return dynamicListResponse;
    }

    //封装返回 动态信息
    public DynamicResponse getDynamicResponse(Dynamic dynamic,Integer userId){
        DynamicResponse dynamicResponse = new DynamicResponse();
        BeanUtils.copyProperties(dynamic,dynamicResponse);
        //根据用户id获取用户信息
        UserInfoVo userBaseInfoVo = userService.getUserBaseInfoVo(dynamic.getUserId());
        dynamicResponse.setUserInfoVo(userBaseInfoVo);
        //根据图片json获取图片信息
        List<Map<String, Object>> pictureUrls = userService.getPictureUrls(dynamic.getPictureAssociationId());
        if(pictureUrls!=null && pictureUrls.size()>0){
            dynamicResponse.setPictureUrls(pictureUrls);
        }
        //处理发布时间
        String releaseTime = RelativeDateFormat.format(dynamic.getCreateTime());
        dynamicResponse.setReleaseTime(releaseTime);
        ThumbInfoListRequest thumbInfoListRequest = new ThumbInfoListRequest();
        thumbInfoListRequest.setThumbAssociationId(dynamic.getDynamicId());
        thumbInfoListRequest.setThumbAssociationType(3);
        JSONObject thumbInfo = thumbInfoService.getThumbInfo(dynamic.getDynamicId(), 3, userId);
        dynamicResponse.setThumbInfo(thumbInfo);
        return dynamicResponse;
    }

}
